package com.ruoyi.eservice.service.impl;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.eservice.mapper.DictMapper;
import com.ruoyi.eservice.pojo.entity.Dict;
import com.ruoyi.eservice.service.IDictService;

/**
 * 城市查询Service业务层处理
 *
 * @author fancheng
 * @date 2022-11-10
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService
{
    @Autowired
    private DictMapper dictMapper;


    public <T> QueryWrapper<T> createSelWrapper(Class<T> type) {
        QueryWrapper<T> queryWrapper = Wrappers.query();
        queryWrapper.eq("del_flag", "0");
        return queryWrapper;
    }

    /**
     * 查询城市查询
     *
     * @param dictId 城市查询主键
     * @return 城市查询
     */
    @Override
    public Dict selectDictByDictId(Long dictId)
    {
        return dictMapper.selectById(dictId);
    }

    /**
     * 查询城市查询列表
     *
     * @param dict 城市查询
     * @return 城市查询
     */
    @Override
    public List<Dict> selectDictList(Dict dict)
    {
        QueryWrapper<Dict> queryWrapper = createSelWrapper(Dict.class);
        queryWrapper.lambda()
                .eq(StringUtils.isNotNull(dict.getDictId()), Dict::getDictId, dict.getDictId())
                .eq(StringUtils.isNotNull(dict.getParentId()), Dict::getParentId, dict.getParentId())
                .like(StringUtils.isNotEmpty(dict.getName()), Dict::getName, dict.getName())
                .eq(StringUtils.isNotEmpty(dict.getDelFlag()), Dict::getDelFlag, dict.getDelFlag())
                .eq(StringUtils.isNotNull(dict.getLevel()), Dict::getLevel, dict.getLevel())
        ;
        return dictMapper.selectList(queryWrapper);
    }

    /**
     * 新增城市查询
     *
     * @param dict 城市查询
     * @return 结果
     */
    @Override
    public int insertDict(Dict dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        dict.setCreateTime(DateUtils.getNowDate());
        dict.setUpdateBy(SecurityUtils.getUsername());
        dict.setUpdateTime(DateUtils.getNowDate());
        int rows = dictMapper.insert(dict);
        return rows;
    }

    /**
     * 修改城市查询
     *
     * @param dict 城市查询
     * @return 结果
     */
    @Override
    public int updateDict(Dict dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        dict.setUpdateTime(DateUtils.getNowDate());

        return dictMapper.updateById(dict);
    }

    /**
     * 批量删除城市查询
     *
     * @param dictIds 需要删除的城市查询主键
     * @return 结果
     */
    @Override
    public int deleteDictByDictIds(Long[] dictIds)
    {
        return dictMapper.deleteBatchIds(Arrays.asList(dictIds));
    }

    /**
     * 删除城市查询信息
     *
     * @param dictId 城市查询主键
     * @return 结果
     */
    @Override
    public int deleteDictByDictId(Long dictId)
    {
        return dictMapper.deleteById(dictId);
    }
}
